//
//  FirstWeChatLoginView.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/25.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "FirstWeChatLoginView.h"
#import <MBProgressHUD.h>
#import "VervifyPhone.h"
#import "BLHeader.h"
#import "ThirdLoginAddTelNet.h"

@interface FirstWeChatLoginView ()
@property (weak, nonatomic) IBOutlet UITextField *telField;
@property (weak, nonatomic) IBOutlet UIButton *commitBtn;
@property (weak, nonatomic) IBOutlet UILabel *saleLabel;
@property (weak, nonatomic) IBOutlet UIButton *saleBtn;
@property (weak, nonatomic) IBOutlet UILabel *storeLabel;
@property (weak, nonatomic) IBOutlet UIButton *customBtn;
@property (weak, nonatomic) IBOutlet UILabel *customLabel;
@property (weak, nonatomic) IBOutlet UIButton *storeBtn;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (nonatomic, assign) NSInteger userClass;

@end

@implementation FirstWeChatLoginView

+ (instancetype)defaultFirstView {
    FirstWeChatLoginView *first = [[[NSBundle mainBundle] loadNibNamed:@"FirstWeChatLoginView" owner:nil options:nil] lastObject];
    return first;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupUI];
}

- (void)setupUI {
    self.codeBtn.layer.cornerRadius = 5;
    self.commitBtn.layer.cornerRadius = 5;
    [self.storeBtn setSelected:YES];
    self.contentView.layer.cornerRadius = 5;
    self.storeLabel.textColor = kAppThemeColor;
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)];
    [self addGestureRecognizer:tap];
    self.userClass = 1
    ;
}

- (void)hideKeyBoard {
    [self endEditing:YES];
}

- (IBAction)checkStore:(UIButton *)sender {
    if(sender.isSelected) {
        return;
    }
    [sender setSelected:YES];
    [self.saleBtn setSelected:NO];
    self.storeLabel.textColor = kAppThemeColor;
    self.saleLabel.textColor = [UIColor blackColor];
    self.customLabel.textColor = [UIColor blackColor];
    [self endEditing:YES];
    self.userClass = 1;
}

- (IBAction)checkSale:(UIButton *)sender {
    if(sender.isSelected) {
        return;
    }
    [sender setSelected:YES];
    [self.storeBtn setSelected:NO];
    self.storeLabel.textColor = [UIColor blackColor];
    self.customLabel.textColor = [UIColor blackColor];
    self.saleLabel.textColor = kAppThemeColor;
    [self endEditing:YES];
    self.userClass = 2;
}

- (IBAction)checkCustom:(UIButton *)sender {
    if(sender.isSelected) {
        return;
    }
    [sender setSelected:YES];
    [self.storeBtn setSelected:NO];
    [self.saleBtn setSelected:NO];
    self.saleLabel.textColor = [UIColor blackColor];
    self.storeLabel.textColor = [UIColor blackColor];
    self.customLabel.textColor = kAppThemeColor;
    [self endEditing:YES];
    self.userClass = 3;
}

#pragma mark 获取验证码
- (IBAction)clickCode:(id)sender {
    NSString *tel = self.telField.text;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
    if(!tel || [tel length] == 0) {
        hud.mode = MBProgressHUDModeText;
        hud.label.text = @"请输入手机号！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    if([tel length] != 11 || ![VervifyPhone vervifyTellphoneNumber:tel]) {
        hud.mode = MBProgressHUDModeText;
        hud.label.text = @"请输入正确的手机号！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    hud.mode = MBProgressHUDModeIndeterminate;
    
    [ThirdLoginAddTelNet getCode:tel completer:^(id object, NSError *error) {
        if(error) {
            hud.mode = MBProgressHUDModeText;
            hud.label.text = [error localizedDescription];
            [hud hideAnimated:YES afterDelay:2];
        }else {
            [hud hideAnimated:YES];
        }
    }];
    [self.codeBtn setTitle:@"120s后重新获取" forState:UIControlStateNormal];
    self.codeBtn.userInteractionEnabled = NO;
    NSInteger time = 120;
    [self codeTimer:time];
    
}

- (void) codeTimer:(NSInteger) time {
    time--;
    __weak typeof (self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        if (time == 0) {
            [weakSelf.codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
            weakSelf.codeBtn.userInteractionEnabled = YES;
            return;
        }else {
            [weakSelf.codeBtn setTitle:[NSString stringWithFormat:@"%lis后重新获取",time] forState:UIControlStateNormal];
            [weakSelf codeTimer:time];
        }
    });
}


#pragma mark 提交数据
- (IBAction)clickCommit:(id)sender {
    NSString *tel = self.telField.text;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
    if(!tel || [tel length] == 0) {
        hud.mode = MBProgressHUDModeText;
        hud.label.text = @"请输入手机号！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    if([tel length] != 11 || ![VervifyPhone vervifyTellphoneNumber:tel]) {
        hud.mode = MBProgressHUDModeText;
        hud.label.text = @"请输入正确的手机号！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    
    NSString *code = _codeField.text;
    if (!code || [code length] == 0) {
        hud.mode = MBProgressHUDModeText;
        hud.label.text = @"请输入验证码！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    hud.mode = MBProgressHUDModeIndeterminate;
    NSString *openID = [[NSUserDefaults standardUserDefaults] objectForKey:WECHATE_OPENID];
    NSInteger loginWay = [[[NSUserDefaults standardUserDefaults] objectForKey:LOGINWAY] integerValue];
    NSDictionary *param;
    switch (loginWay) {
        case 1:
            param = @{@"userTel" : tel, @"msgCode" : code, @"userClass" : [NSString stringWithFormat:@"%li",self.userClass] ,@"openid" :openID};
            break;
        case 2:
            param = @{@"userTel" : tel, @"msgCode" : code, @"userClass" : [NSString stringWithFormat:@"%li",self.userClass] ,@"openId" :openID};
            break;
        case 3:
            param = @{@"userTel" : tel, @"msgCode" : code, @"userClass" : [NSString stringWithFormat:@"%li",self.userClass] ,@"uid" :openID};
            break;
            
        default:
            break;
    }
        [ThirdLoginAddTelNet postNewInfo:param completer:^(id resultObject, NSError *error) {
        hud.mode = MBProgressHUDModeText;
        if(resultObject) {
            [self dismissView];
            [[NSNotificationCenter defaultCenter] postNotificationName:UPDATETABTITLE object:nil];
        }else {
            hud.label.text = [error localizedDescription];
            [hud hideAnimated:YES afterDelay:2];
        }
    }];
}

- (void)show:(UIView *)whereView animation:(BOOL)isAnimation {
    [whereView addSubview:self];
    if (isAnimation) {
         self.frame = CGRectMake(0, KScreenHeight, KScreenWidth, KScreenHeight);
        [UIView animateWithDuration:0.2 animations:^{
            self.frame = [UIScreen mainScreen].bounds;
        }];
    }
}

- (void)dismissView {
    [self removeFromSuperview];
    [UIView animateWithDuration:0.2 animations:^{
        self.frame = CGRectMake(0, KScreenHeight, KScreenWidth, KScreenHeight);
    }];
    
    
}
@end
